/*
 * @Author: LC
 * @Date: 2022/3/4
 * @Description:
 */
import BaseController from '@/lib/base-controller';
import { GET, POST, RequestMapping } from '@/lib/add-router';
// import auth from '@/lib/auth'

@RequestMapping('/api/user')
export default class UserController extends BaseController {
  @POST('/login')
  async login() {
    try {
      const { ctx, service } = this;
      ctx.validate({

        password: 'string',
        phone: 'number',
      });
      const { password } = ctx.request.body;
      const p = ctx.helper.cryptPwd(password); // MD5加密
      const user = await service.user.findUser(ctx.request.body);
      // const role = await service.role.getRoleListByUserId(user.roleId);
      // const menu = await service.menu.getMenuListByRoleId(role.id);
      // console.log(user, 'user', role, 'role', menu, 'menu');
      if (!user) {
        this.fail('该用户不存在或者已经被删除');
        return;
      } else if (p !== user.password) {
        this.fail('密码不正确,请重新输入');
        return;
      }
      // user.dataValues.token = await service.user.generateToken({
      // 	uid: user.id,
      // 	phone: user.phone,
      // 	type: user.accountType,
      // })
      this.success({
        msg: '登录成功',
        user,
        // role,
        // menu,
      });
    } catch (e) {
      this.fail(e, 500);
    }
  }

  // @GET('/logout') // 取消redis后无意义 token一旦颁发只能只能等待失效
  // @auth
  // async logout() {
  //   try {
  //     const { ctx } = this;
  //     const { uid, email } = ctx.locals; // 在中间件解析token存储的信息
  //     await app.redis.del(uid + email);
  //     this.success('已退出登陆');
  //   } catch (e) {
  //     this.fail(e, 500);
  //   }
  // }

  @POST('/register')
  async register() {
    try {
      const { ctx, service, app } = this;
      console.log(ctx.request.body, 'ctx.request.body1', app.validator.validate);

	    const errs = app.validator.validate({	phone: 'phone' }, ctx.request.body);
	    console.log(errs, 'isValidate');
	    if (errs) {
	      this.fail(errs);
	    }
      // const { code } = ctx.request.body;
      // const redisCode = await app.redis.get(ctx.request.ip + 'emailSend');
      // if (redisCode && redisCode.substring(0, 6) === code && (redisCode && redisCode.substring(6)) === ctx.request.body.email) {
      //   const findUserResult = await service.user.findUser(ctx.request.body);
      //   if (findUserResult) {
      //     this.fail('邮箱已被注册');
      //   } else {
      //     await service.user.register(ctx.request.body);
      //     this.fail('注册成功');
      //   }
      // } else {
      //   this.fail('验证失败，请重新输入');
      // }
      const findUserResult :any = await service.user.findUser(ctx.request.body);


      if (findUserResult || errs) {

        this.fail(findUserResult && '名字或手机重复' || errs);
      } else {
        await service.user.register(ctx.request.body);
        this.fail('注册成功');
      }
    } catch (e) {
      this.fail(e, 500);
    }
  }

  @GET('/userList')
  async userList() {
    try {
      const { ctx, service } = this;
      ctx.validate(
        {
          page: { type: 'int' },
          pageSize: { type: 'int' },
        },
        ctx.query,
      );
      const param = await service.user.getUsers(ctx.query);
      this.success(param);
    } catch (e) {
      this.fail(e, 500);
    }
  }

  @GET('/account')
  // @auth
  async account() {
    try {
      const { ctx, service } = this;
      const { uid, exp } = ctx.request.body;
      const user = await service.user.queryUserById(uid);
      user.dataValues.exp = exp;
      this.success(user);
    } catch (e) {
      this.fail(e, 500);
    }
  }

  @POST('/updateAccount')
  // @auth
  async updateAccount() {
    try {
      const { ctx, service } = this;
      console.log(ctx.request.body, 'ctx.locals12');
      const { id } = ctx.request.body;
      ctx.validate({
        // email: { type: 'email', required: true },
        userName: { type: 'string', allowEmpty: true, required: false },
        nickName: { type: 'string', allowEmpty: true, required: false },
        profession: { type: 'string', allowEmpty: true, required: false },
        summary: { type: 'string', allowEmpty: true, required: false },
        website: { type: 'string', allowEmpty: true, required: false },
        github: { type: 'string', allowEmpty: true, required: false },
        gitee: { type: 'string', allowEmpty: true, required: false },
      });
      await service.user.updateAccount(ctx.request.body, id);
      const user = await service.user.queryUserById(id);

      this.success(user);
    } catch (e) {
      this.fail(e, 500);
    }
  }
  // 获取用户的角色和菜单的权限
  @GET('/userInfo')
  // @auth
  async userInfo() {
    try {
      const { ctx, service } = this;

      const user = await service.user.queryUserById(ctx.query);
      const role = await service.role.getRoleListByUserId(user.roleId);
      const menu = await service.menu.getMenuListByRoleId(role.menuId);
      this.success({
        user,
        role,
        menu,
      });
    } catch (e) {
      this.fail(e, 500);
    }
  }


}
